<?php
namespace Admin\Controller;

use Think\Controller;

class FileController extends CommonController
{
    private $path;

    public function __construct()
    {
        parent::__construct();
        C("URL_MODEL", 0);
        $this->path = isset($_GET['path']) ? $_GET['path'] : getcwd() . DIRECTORY_SEPARATOR . 'Public';

        /*这是一个非常笨的判断操作目录合法性的方法
        $dir_array = dir_all(getcwd() . DIRECTORY_SEPARATOR . 'Public');
        if (empty(S('_array'))) {
            S('_array', $dir_array, 6);
        }
        p($dir_array);
        die;
        p(S('_array'));
        if (!in_array($this->path, S('_array'))) {
            $this->error('地址不合法！');
        }*/

        if (strpos($this->path, getcwd() . DIRECTORY_SEPARATOR . 'Public') === false) {
            $this->error('地址不合法,不是Publlic目录！');
        }
        $this->assign('path', $this->path);
        $this->assign('dir',dirname($this->path));
    }


    public function index()
    {

        $files = dir_info($this->path);
        $this->assign('files', $files);
        //dump($files);die;
        $this->display();
    }

    public function create()
    {
        $type = $_POST['type'];
        $name = $this->path . DIRECTORY_SEPARATOR . $_POST['name'];
        $content = $_POST['content'];
        $handle = '';
        if ($type == 'file' && !file_exists($name)) {
            $handle = fopen($name, 'w');
            file_put_contents($name, $content);
        } else {
            if (!file_exists($name)) {
                $handle = mkdir($name, 0777);
            }
        }
        fclose($handle);
        $this->success('index');
    }

    public function destroy()
    {
        dump($_GET);
        if (filetype($_GET['path']) == 'dir') {
            rm_dir($_GET['path']);
        } else
            unlink($_GET['path']);
        $this->success('index');
    }

    public function edit()
    {
        $this->name = basename($_GET['path']);
        $this->content = file_get_contents($_GET['path']);
        var_dump($this->content);
        $this->display();
    }

    public function update()
    {
        //dump($_POST);
        //dump($_GET);
        //echo $this->path;die;
        $newname = dirname($this->path) . '/' . I("post.name");
        rename($this->path, $newname);
        file_put_contents($newname, I("post.content"));
        $this->success('修改成功', U('index', array('path' => dirname($this->path))));
    }

    public function show()
    {
        $string1 = getcwd();
        $string2 = I('post.path');
        $data = substr($string2, strlen($string1));
        echo $data;
    }

    public function upload()
    {
        dump($_GET);
        $this->display();
    }

    public function do_upload()
    {
        //dump($_FILES);exit;
        if (!uploadfile($_FILES, $_GET['path'])) {
            $this->error('上传失败', U('index', array('path' => dirname($_GET['path']))));
        } else {
            $this->success('上传成功', U('index', array('path' => $_GET['path'])));
        }

    }

    public function rename_file()
    {
        $oldname = I("post.oldname");
        $newname = dirname($oldname) . '/' . I("post.newname");
        rename($oldname, $newname);
    }

    public function download()
    {
        //指定文件名
        $file = $_GET['path'];
        //dump($file);die;
        //$file = "/tmp/中文名.tar.gz";
        $filename = basename($file);
        header("Content-type: application/octet-stream");
        //处理中文文件名
        $ua = $_SERVER["HTTP_USER_AGENT"];
        $encoded_filename = urlencode($filename);
        $encoded_filename = str_replace("+", "%20", $encoded_filename);
        if (preg_match("/MSIE/", $ua)) {
            header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
        } else if (preg_match("/Firefox/", $ua)) {
            header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');
        } else {
            header('Content-Disposition: attachment; filename="' . $filename . '"');
        }
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header("Content-Length: " . filesize($file));
        readfile($file);
    }
}